package edu.drexel.jrexel2d.objects2D;

import edu.drexel.jrexel2d.scripting.*;
import edu.drexel.jrexel2d.animation.Sprite;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaError;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
import org.luaj.vm2.lib.jse.JsePlatform;

public class Object2D_AI extends Object2D implements IAIFunctionality {

	/**
	 * @param args
	 */

    private String AI_Script;
    
    public Object2D_AI(Sprite s, int i) {
    	super(s, i);
    	AI_Script = DEFAULT_AI_SCRIPT;
    }
    
    public Object2D_AI(Sprite s, int i, String inAIScript) {
    	super(s, i);
    	AI_Script = inAIScript;
    }

	@Override
	public void setAIScript(String inAIScript) {
		AI_Script = inAIScript;
	}

	@Override
	public String getAIScript() {
		return AI_Script;
	}

	@Override
	public void executAIScript() {
		if(AI_Script != null)
	      {
	    	try{
		     Globals globals = JsePlatform.standardGlobals();
		
		     globals.get("dofile").call(LuaValue.valueOf(AI_Script));
		
		     LuaValue myMethod = globals.get("PerformAI");
		
		     LuaValue retvals = myMethod.call(CoerceJavaToLua.coerce(this));
		     
	    	}
	    	catch(LuaError e){
	    		e.printStackTrace();
	    	}
	       }
		
	}
	
	public void action(){
		System.out.println("Performed action");
	}
	
	

}
